package com.lhframework.upload.controller;

import com.lhframework.upload.model.ImageFile;
import com.lhframework.upload.service.ImageFileService;
import com.lhframework.upload.uploader.FileUploader;
import io.swagger.annotations.ApiOperation;
import lh.base.support.model.AjaxResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.DecimalFormat;
import java.util.Optional;
import java.util.UUID;

/**
 * Created by lh on 2017/8/2.
 */
@RestController
@RequestMapping("/upload")
public class UploadController {
    @Autowired
    private FileUploader fileUploader;
    @Autowired
    private ImageFileService imageFileService;

    @ApiOperation(value = "上传图片文件")
    @RequestMapping(value = "/image", method = RequestMethod.POST)
    public AjaxResponse imageUpload(@RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        String suffix = getFileSuffix(file.getOriginalFilename());
        if (!suffix.equalsIgnoreCase(".JPEG") && !suffix.equalsIgnoreCase(".JPG") && !suffix.equalsIgnoreCase(".GIF")
                && !suffix.equalsIgnoreCase(".PNG") && !suffix.equalsIgnoreCase(".BMP")) {
            return AjaxResponse.fail().msg("请上传图片文件");
        }
        String imageUrl = fileUploader.upload(file.getBytes(), translateFileName(file.getOriginalFilename()));
        ImageFile imageFile = new ImageFile(imageUrl, file.getContentType(), translateFileSize(file.getSize()));
        imageFileService.add(imageFile);
        return AjaxResponse.ok().data(imageUrl);
    }

    private String getFileSuffix(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }

    private String translateFileName(String fileName) {
        if (Optional.ofNullable(fileName).isPresent() && fileName.contains(".")) {
            return String.valueOf(UUID.randomUUID()).replaceAll("-", "") + getFileSuffix(fileName);
        } else {
            return fileName;
        }
    }

    private String translateFileSize(long fileSize) {
        long KB = 1024;
        long MB = KB * 1024;
        long GB = MB * 1024;
        double size = Double.valueOf(fileSize);
        double kb_size = size / KB;
        double mb_size = size / MB;
        double gb_size = size / GB;
        DecimalFormat df = new DecimalFormat("#.00");
        if (size < KB) {
            return size + "B";
        } else if (size < MB) {
            return df.format(kb_size) + "KB";
        } else if (size >= MB && size < GB) {
            return df.format(mb_size) + "MB";
        } else if (size >= GB) {
            return df.format(gb_size) + "GB";
        }
        return null;
    }
}
